package com.dzl.base.controller;

import com.alibaba.fastjson.JSONObject;
import com.dzl.admin.pojo.vo.LoginSysAdminVo;
import com.dzl.config.exception.CommonException;
import com.dzl.supplier.pojo.vo.WelfareSupplierLoginVo;
import com.dzl.userwx.pojo.vo.LoginWxUserInfoVo;
import com.dzl.util.EmptyUtils;
import com.dzl.util.HttpCode;
import com.dzl.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;

@Slf4j
public abstract class BaseController {

    public static BaseController baseController;

    @PostConstruct
    public void init() {
        baseController = this;
    }

    /**
     * @return HttpServletRequest
     * @Description:获取 request
     * @exception:
     * @author: liufan
     * @time:2019年3月22日 上午11:00:45
     */
    public static HttpServletRequest getHttpServletRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
        return request;
    }

    /**
     * 通过头部token获取redis中缓存的【后端】用户信息
     * @param
     * @return com.dzl.userwx.pojo.vo.LoginUserVo
     * @date 2020/11/24 21:23
     * @author lishunxing
     */
    public LoginSysAdminVo getAdminInfo() {
        HttpServletRequest request = getHttpServletRequest();
        String token = request.getHeader("token");
        if (StringUtils.isBlank(token)) {
        	//throw new TokenException("登录信息已失效");
            throw new CommonException(HttpCode.NO_LOGIN);
        }
        LoginSysAdminVo loginInfo = null;
        String s = "";
        try {
            s = RedisUtil.get(token).toString();
            loginInfo = JSONObject.parseObject(s, LoginSysAdminVo.class);
            log.info("后端登录成功，用户信息======= : " + s);
        } catch (Exception e) {
        	log.info("登录信息已失效，请退出再重新登录 content : " + s);
        	log.info("登录信息已失效，请退出再重新登录 content : " + e.getMessage());
            //throw new TokenException("登录信息已失效，请退出再重新登录 content : " + s);
            throw new CommonException(HttpCode.NO_LOGIN);
        }
        return loginInfo;
    }

    /**
     * 获取登录供应商的详细信息
     * @date 2021/10/11 17:26
     * @author caobucao
     * @return com.dzl.util.JsonResult<com.dzl.supplier.pojo.vo.WelfareSupplierLoginVo>
     */
    public WelfareSupplierLoginVo getLoginSupplierInfo() {
        HttpServletRequest request = getHttpServletRequest();
        String token = request.getHeader("token");
        if (StringUtils.isBlank(token)) {
           // throw new TokenException("登录信息已失效");
            throw new CommonException(HttpCode.NO_LOGIN);
        }
        WelfareSupplierLoginVo supplierLoginVo = null;
        String s = "";
        try {
            s = RedisUtil.get(token).toString();
            supplierLoginVo = JSONObject.parseObject(s, WelfareSupplierLoginVo.class);
            log.info("后端登录成功，用户信息======= : " + s);
        } catch (Exception e) {
            log.info("登录信息已失效，请退出再重新登录 content : " + s);
            log.info("登录信息已失效，请退出再重新登录 content : " + e.getMessage());
            //throw new TokenException("登录信息已失效，请退出再重新登录 content : " + s);
            throw new CommonException(HttpCode.NO_LOGIN);
        }
        return supplierLoginVo;
    }


    /**
     * 通过头部token获取redis中缓存的【前端】用户信息（小程序）
     * @author ChenLong
     * @date 2021-05-27 11:51:43
     * @return com.dzl.userwx.pojo.vo.WelfareUserWxAuthVo
     **/
    public LoginWxUserInfoVo getWxUserInfo() {
        HttpServletRequest request = getHttpServletRequest();
        String token = request.getHeader("token");
        LoginWxUserInfoVo loginInfo = null;
        if(EmptyUtils.isEmpty(token)){//数据测试用，后面会删掉
            /*token = CookieUtils.getCookieValue(request,"token");
            if(EmptyUtils.isEmpty(token)){
                loginInfo = new LoginMemberVo();
                loginInfo.setId(2315);
                loginInfo.setMerchantId(1625753);
                loginInfo.setCompanyId(1595);
                loginInfo.setPhone("18062754646");
                loginInfo.setMemberName("刘官君");
                return  loginInfo;
            }*/
            return null;
        }
        String s = "";
        try {
            s = RedisUtil.get(token).toString();
            loginInfo = JSONObject.parseObject(s,LoginWxUserInfoVo.class);
            log.info("前端登录成功，用户信息======= : " + s);
        }catch (Exception e){
            //throw new TokenException("登录信息已失效，请退出再重新登录 content : " + s);
            throw new CommonException(HttpCode.NO_LOGIN);
        }
        return loginInfo;
    }

    public Integer getWxUserId() {
        LoginWxUserInfoVo loginInfo = getWxUserInfo();
        if(EmptyUtils.isEmpty(loginInfo)){
            return null;
        }
        return loginInfo.getId();
    }

}
